package net.huashitong.supervision.AppNew.appService;

import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.entity.BaseEntity;
import com.sirdc.modules.core.service.StringService;
import com.sirdc.modules.sys.dao.SysLoginDao;
import com.sirdc.modules.sys.dao.SysUserDao;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.AppNew.DTO.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * App  调用用户和部门的service
 */
@Service()
public class AppUserService {

    @Autowired
    private  SysUserDao userDao;
    @Autowired
    private SysLoginDao sysLoginDao;
    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private RedisService  redisService;


    /**
     * 根据手机号获取用户名，用户信息
     * @param telphone
     * @return
     */
    public List<UserDTO> getUserByPhone(String telphone )
    {
        SysUserFilter filter = new SysUserFilter();
        filter.setMobile(telphone);
        List<SysUser> users = userDao.queryList(filter);
        List<UserDTO> listDto = new ArrayList<>();

        for (SysUser user : users) {
            SysLogin login = sysLoginDao.getById(user.getSysId());
            UserDTO dto = new UserDTO();
            dto.setUsername(login.getUsername());
            dto.setDeptId(login.getDeptId());
            dto.setRoleId(login.getRoleId());
            dto.setMobile(user.getMobile());
            dto.setRealName(user.getName());
            dto.setUserId(login.getSysId());
            dto.setPersonSignal(user.getPersonSignal());
            dto.setState(login.getState());
            dto.setUserType(login.getUserType());
            dto.setDeptName("");
            SysDept deptEntity = sysDeptService.getById(login.getDeptId());
            if(ObjectUtils.isNotBlank(deptEntity))
            {
                dto.setDeptName(deptEntity.getDeptName());

            }
            listDto.add(dto);
        }

        return  listDto;
    }


    public void saveAppLogin(UserDTO dto)
    {
        String userId = dto.getUserId();

        String userJson = JSONObject.toJSONString(dto);


        redisService.set("applogin:dc:"+userId,userJson);


    }







}
